package com.mairuide._frame.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * @Description
 * @auther 刘中华
 * @create 2019-04-10 10:26
 */
public class MacUtils {
    public static String getMacId()  {
        StringBuffer sb = null;
        try {
            InetAddress ia = InetAddress.getLocalHost();
            byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
            sb = new StringBuffer("");
            for(int i=0; i<mac.length; i++) {
                if(i!=0) {
                    sb.append("-");
                }
                int temp = mac[i]&0xff;
                String str = Integer.toHexString(temp);
                if(str.length()==1) {
                    sb.append("0"+str);
                }else {
                    sb.append(str);
                }
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (SocketException e) {
            e.printStackTrace();
        }finally {
            if (sb!=null){
                return sb.toString().toUpperCase();
            }else{
                return null;
            }
        }
    }
    public static void main(String[] args){
        System.out.println(getMacId());
    }
}
